﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using SmartMeterParamFrame;

namespace SchemeManager
{
    /// <summary>
    /// SelectItem.xaml 的交互逻辑
    /// </summary>
    public partial class SelectItem : Window
    {
        /// <summary>
        /// 选择的项目
        /// </summary>
        public SmartMeterParamFrame.Model.FAinfo SelectedItem { get; set; }
        /// <summary>
        /// 所有选择的项目
        /// </summary>
        public List<SmartMeterParamFrame.Model.FAinfo> SelectedItems { get; set; }

        private DataGrid[] grids = null;
        public SelectItem()
        {
            InitializeComponent();
            grids = new DataGrid[] { 
            //Grid_Basic,Grid_CostControl,Grid_FirstPara,Grid_SecondPara,Grid_MeterKeyDown
            Grid_Basic,Grid_FirstPara,Grid_SecondPara
            };
        }

        public SelectItem(int paraType)
            : this()
        {
            if (paraType < 0) paraType = 0;
            //paraType++;
            int pos = paraType % TabMain.Items.Count;
            ////转换
            //if (pos == 0)
            //{
            //    pos = 2;
            //}
            //else
            //{
            //    if (pos == 2)
            //        pos = 0;
            //}
            //转换完毕
            //设置选择标签
            TabMain.SelectedIndex = pos;
            //隐藏其它标签
            foreach (TabItem item in TabMain.Items)
            {
                if (item.IsSelected == false)
                    item.Visibility = System.Windows.Visibility.Hidden;
            }
            //设置GRID为单选模式
            grids[pos].SelectionMode = DataGridSelectionMode.Single;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Button btnSource = sender as Button;
            if (btnSource == null) return;

            int selectedIndex = TabMain.SelectedIndex;
            if (selectedIndex == -1) return;
            DataGrid currentGrid = grids[selectedIndex];

            switch (btnSource.Name)
            {
                case "btnSelectAll":
                    {
                        currentGrid.SelectAll();
                    }
                    break;
                case "btnSelectNone":
                    {
                        currentGrid.UnselectAll();
                    }
                    break;
                case "btnSelectDifferent":
                    //{
                    //    foreach (DataGridRow row in currentGrid.Items)
                    //    {
                    //        row.IsSelected = !row.IsSelected;
                    //    }
                    //}
                    break;
                case "btnOk":
                    {
                        SelectedItems = new List<SmartMeterParamFrame.Model.FAinfo>();
                        SmartMeterParamFrame.Model.FAinfo newItem = null;


                        foreach (System.Xml.XmlElement el in currentGrid.Items)
                        {
                            if (el.Attributes["Data"].Value != "")
                            {
                                newItem = new SmartMeterParamFrame.Model.FAinfo();
                                newItem.BsCode = el.Attributes["Mark"].Value;              //数据标识
                                newItem.PrjName = el.Attributes["Name"].Value;             //项目名称
                                newItem.DataDomainDP =  el.Attributes["Data"].Value;          //数据域
                                newItem.ReviewType = (EnumCheckType)Enum.Parse(typeof(EnumCheckType), el.GetAttribute("ReviewType"), true);
                                //转换
                                string paraType = el.ParentNode.Attributes["Name"].Value;
                                if (paraType == "基本参数")
                                    paraType = "其他操作";
                                newItem.PrjType = (SmartMeterParamFrame.EnumFAType)Enum.Parse(typeof(SmartMeterParamFrame.EnumFAType), paraType);  //项目
                                SelectedItems.Add(newItem);
                            }
                        }
                        if (SelectedItems.Count > 0)
                            SelectedItem = SelectedItems[0];
                        DialogResult = true;
                    }
                    break;
                case "btnCancel":
                    {
                        DialogResult = false;
                    }
                    break;
            }
        }
    }
}
